//
// This file is part of the GNU ARM Eclipse distribution.
// Copyright (c) 2014 Liviu Ionescu.
//

// ----------------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include "diag/Trace.h"

// ----------------------------------------------------------------------------
//
// Standalone STM32F0 empty sample (trace via NONE).
//
// Trace support is enabled by adding the TRACE macro definition.
// By default the trace messages are forwarded to the NONE output,
// but can be rerouted to any device or completely suppressed, by
// changing the definitions required in system/src/diag/trace_impl.c
// (currently OS_USE_TRACE_ITM, OS_USE_TRACE_SEMIHOSTING_DEBUG/_STDOUT).
//

// ----- main() ---------------------------------------------------------------

// Sample pragmas to cope with warnings. Please note the related line at
// the end of this function, used to pop the compiler diagnostics status.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wmissing-declarations"
#pragma GCC diagnostic ignored "-Wreturn-type"

#include "main.h"
#include "utils.h"
#include "message_queue.h"
#include "ctimer.h"
#include "shell.h"
#include "serial.h"
#include "io.h"
#include "remote.h"
#include "sleep.h"
#include "io/watchdog.h"

bool _isDebugOn;

static const Shell_Cmd_t const _shellCmds[] =
{
        {"help", Shell_Help},
        {"test", Shell_Test},
        {"SETIO", IO_ShellSet},
        {"GETIO", IO_ShellGet},
        {"", NULL},
};


static void _MsgDeliver(MsgQueue_Msg_t *msg);
static void _Init(void);

int
main(int argc, char* argv[])
{
  // At this stage the system clock should have already been configured
  // at high speed.
    _Init();
    Utils_Init();
    MsgQueue_Init(_MsgDeliver);
    Shell_Init(_shellCmds);
    Ctimer_Init();
    Serial_Init();

    Remote_Init();
    IO_Init();
    Sleep_Init();

    debug("GPS-LOCK_IO %s %s\r\n", __DATE__, __TIME__);

  // Infinite loop
  while (1)
    {
       // Add your code here.
      MsgQueue_Service();

      PWR_EnterSleepMode(PWR_SLEEPEntry_WFI);
    }
}

static void _Init(void)
{
    _isDebugOn = TRUE;
}

static void _OnIntPrint(MsgQueue_Msg_t *msg)
{
    const char *str;
    unsigned int i;

    str = (const char *) msg->data;
    for(i = 0; i < msg->size; i++)
    {
        mprintf("%c", str[i]);
    }
}

static void _MsgDeliver(MsgQueue_Msg_t *msg)
{
    switch(msg->id)
    {
    case MsgQueue_Id_ShellLineIn:
        Shell_OnLine(msg);
        break;

    case MsgQueue_Id_CtimerFire:
        Ctimer_Fire(msg);
        break;

    case MsgQueue_Id_IoUpdate:
        break;

    case MsgQueue_Id_IoDevChange:
        break;

    case MsgQueue_Id_Vibration:
        IO_OnVibrationUpdate(msg);
        break;

    case MsgQueue_Id_IoNotyfyRemote:
        Remote_WakeUp();
        IO_OnNotify(msg);
        break;

    case MsgQueue_Id_IntPrint:
        _OnIntPrint(msg);
        break;

    case MsgQueue_Id_WatchDogReset:
        WatchDog_OnReset();
        break;

    case MsgQueue_Id_Monitor:
        IO_OnMonitor();
        break;

    default:
        debug("Unknown msg, id: %d\r\n", msg->id);
        break;
    }
}
#pragma GCC diagnostic pop

// ----------------------------------------------------------------------------
